package gases.as3.librarys.components.factory
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.container.GAlertPanel;
	import gases.as3.librarys.components.ui.container.GPanelWithTitle;
	import gases.as3.librarys.components.ui.container.GScreenPanel;
	import gases.as3.librarys.components.ui.control.GButton;
	import gases.as3.librarys.components.ui.control.group.GButtonGroup;
	import gases.as3.librarys.components.ui.resize.ResizeComponent;
	import gases.as3.util.ComponentName;

	/**
	 *模块视图 
	 * @author helloworld
	 * 
	 */	
	public class ModuleComponent extends ResizeComponent
	{
		public function ModuleComponent()
		{
			_curComponentArrayData = new Array();
			_parentDict = new Dictionary();
			_curcomponentsDict = new Dictionary();
			_screenDict = new Dictionary();
			_moduleDict = new Dictionary();
			_alertDict = new Dictionary();
			_spriteDict = new Dictionary();
			_windowDict = new Dictionary();
			_buttonGroupDict = new Dictionary();
		}
		
		public function creatComponent(value:*):void
		{
			if(value is Array)
			{
				resolveArrayToComponent(value as Array);
			}
		}
		
		/**
		 *通过id查询到组件 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getComponentsById(id:String):DisplayObject
		{
			if(!id)
			{
				return null;
			}
			var dis:DisplayObject = findParent(id);
			return dis;
		}
		
		public function get type():String
		{
			return _type;
		}

		
		private function resolveArrayToComponent(arr:Array):void
		{
			_curComponentArrayData = arr;
			for(var i:int;i<arr.length;i++)
			{
				var obj:Object = arr[i];
				var id:String = obj["id"].toString();
				var localName:String = obj["localname"].toString();
				var parent:String = obj["parent"].toString().replace(/\s/g,"");
				_parentDict[id] = parent;
				var component:GUIComponents = BaseComponentsResolve.retComponent(obj);
				if(component)
				{
					_curcomponentsDict[id] = component;
					if(localName == ComponentName.COMPONENT)
					{
						_spriteDict[id] = component;
					}
					if(localName == ComponentName.SCREENPANEL)
					{
						_type = "screen";
						_screenDict[id] = component;
						vAlign = "middle";
						hAlign = "center";
//						vAlign = obj["valign"].toString();
//						hAlign = obj["halign"].toString();
					}
					if(localName == ComponentName.ALERTPANEL)
					{
						_type = "window";
						_alertDict[id] = component;
						vAlign = obj["valign"].toString();
						hAlign = obj["halign"].toString();
					}
					if(localName == ComponentName.PANEL_WITH_TITLE)
					{
						_type = "module";
						_moduleDict[id] = component;
						vAlign = obj["valign"].toString();
						hAlign = obj["halign"].toString();
					}
					if(localName == ComponentName.PANEL)
					{
						_type = "module";
						_windowDict[id] = component;
						vAlign = obj["valign"].toString();
						hAlign = obj["halign"].toString();
					}
					if(localName == ComponentName.BUTTONGROUP)
					{
						_buttonGroupDict[id] = component;
					}
				}
			}
			addChildToPanel();
		}
		
		private function addChildToPanel():void
		{
			for(var i:int=0;i<_curComponentArrayData.length;i++)
			{
				var obj:Object = _curComponentArrayData[i];
				var id:String = obj["id"].toString();
				var parent:String = _parentDict[id].toString();
				if(parent.length == 0)
				{
					this.addChild(_curcomponentsDict[id] as GUIComponents);
				}
				else
				{
					var gui:GUIComponents;
					if(_moduleDict[parent])
					{
						gui = _moduleDict[parent] as GPanelWithTitle;
						(gui as GPanelWithTitle).pushDisplayObject(_curcomponentsDict[id] as GUIComponents);
					}
					if(_screenDict[parent])
					{
						gui = _screenDict[parent] as GScreenPanel;
						(gui as GScreenPanel).pushDisplayObject(_curcomponentsDict[id] as GUIComponents);
					}
					if(_alertDict[parent])
					{
						gui = _alertDict[parent] as GAlertPanel;
						(gui as GAlertPanel).pushDisplayObject(_curcomponentsDict[id] as GUIComponents);
					}
					if(_spriteDict[parent])
					{
						gui = _spriteDict[parent] as GUIComponents;
						gui.addChild(_curcomponentsDict[id] as GUIComponents);
					}
					if(_windowDict[parent])
					{
						gui = _windowDict[parent] as GUIComponents;
						gui.addChild(_curcomponentsDict[id] as GUIComponents);
					}
					if(_buttonGroupDict[parent])
					{
						gui = _buttonGroupDict[parent] as GButtonGroup;
						if(_curcomponentsDict[id] is GButton)
						{
							(gui as GButtonGroup).addButton(_curcomponentsDict[id] as GButton);
						}
					}
				}
			}
		}
		
		/**
		 *通过id查找父类 
		 * @param id
		 * @return 
		 * 
		 */		
		private function findParent(id:String):GUIComponents
		{
			var spr:GUIComponents;
			if(id)
			{
				if(_curcomponentsDict[id])
				{
					spr = _curcomponentsDict[id] as GUIComponents;
				}
			}
			return spr;
		}
		
		private var _type:String;
		
		private var _vAlign:String;
		private var _hAlign:String;
		
		/**
		 *父类hash 
		 */		
		private var _parentDict:Dictionary;
		private var _curComponentArrayData:Array;
		/**
		 *组件hash 
		 */			
		private var _curcomponentsDict:Dictionary;
		/**
		 *保存guiComponents组件 
		 */	
		private var _spriteDict:Dictionary;
		/**
		 * 保存screen组件 
		 */		
		private var _screenDict:Dictionary;
		/**
		 *保存module组件 
		 */		
		private var _moduleDict:Dictionary;
		/**
		 *保存window组件 
		 */		
		private var _windowDict:Dictionary;
		/**
		 *保存alert组件 
		 */		
		private var _alertDict:Dictionary;
		/**
		 *按钮组组件 
		 */		
		private var _buttonGroupDict:Dictionary;
		
	}
}